package com.example.demo.smartLamp.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.smartLamp.entity.Warn;
import com.example.demo.smartLamp.entity.request.WarnGetAllInfoRequest;
import com.example.demo.smartLamp.entity.response.WarnGetAllInfoResponse;
import com.example.demo.smartLamp.mapper.WarnMapper;
import com.example.demo.smartLamp.service.IWarnService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 祁荣博 胡绍宇
 * @since 2022-07-27
 */
@Service
public class WarnServiceImpl extends ServiceImpl<WarnMapper, Warn> implements IWarnService {

    @Resource
    WarnMapper warnMapper;

    @Override
    public Page<WarnGetAllInfoResponse> warnGetAllInfo(WarnGetAllInfoRequest warnGetAllInfoRequest) throws Exception {
        Page<Warn> warnTablePage = new Page<>();
        warnTablePage.setCurrent(warnGetAllInfoRequest.getCurrentPage());
        warnTablePage.setSize(warnGetAllInfoRequest.getPageSize());

        List<WarnGetAllInfoResponse> warnTables = new ArrayList<>();
        Page<Warn> warnTablePages = warnMapper.selectPage(
                warnTablePage,
                Wrappers.lambdaQuery(Warn.class)
                        .orderByDesc(Warn::getWarnTime)
        );

        for (Warn warnTable : warnTablePages.getRecords()) {

           WarnGetAllInfoResponse warnGetAllInfoResponse = new WarnGetAllInfoResponse();
           warnGetAllInfoResponse.setWarnId(warnTable.getWarnId());
           warnGetAllInfoResponse.setWarnInfo(warnTable.getWarnInfo());
           warnGetAllInfoResponse.setWarnPosition(warnTable.getWarnPosition());
           warnGetAllInfoResponse.setWarnTime(warnTable.getWarnTime());
           warnGetAllInfoResponse.setWarnType(warnTable.getWarnType());

           warnTables.add(warnGetAllInfoResponse);
        }

        Page<WarnGetAllInfoResponse> returnPost = new Page<>();
        returnPost.setRecords(warnTables);
        returnPost.setCountId(warnTablePages.getCountId());
        returnPost.setHitCount(warnTablePages.isHitCount());
        returnPost.setMaxLimit(warnTablePages.getMaxLimit());
        returnPost.setOptimizeCountSql(warnTablePages.isOptimizeCountSql());
        returnPost.setOrders(warnTablePages.getOrders());
        returnPost.setSearchCount(warnTablePages.isSearchCount());
        returnPost.setTotal(warnTablePages.getTotal());
        returnPost.setPages(warnTablePages.getPages());

        return returnPost;
    }
}
